import 'dart:convert';

import 'package:flutter/material.dart';
import '../../helper/routeHelper.dart';
import '../../model/things.dart';
import '../../pages/things/things_helper.dart';
import '../../pages/things/things_select.dart';
import '../../widgets/inputWidget.dart';
import '../../model/inventory.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/someCards.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';

class InventoryAdd0Page extends StatefulWidget {
  @override
  InventoryAdd0Page(this.t1, this.sType, this.x1, this.title, this.added);
  final Inventory? x1;
  final VoidCallback added;
  final Things? t1;
  final int sType;
  final String title;

  @override
  InventoryAdd0PageState createState() => new InventoryAdd0PageState();
}

class InventoryAdd0PageState extends State<InventoryAdd0Page> {
  //List<String> sList={"一般","重要","紧急"}.toList();
  late Inventory X1;
  late Things T1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var now = Helper.DateTimeNowString();
    var uGuid = UIData.CurrentUser!.Guid;

    if (widget.x1 != null) {
      X1 = widget.x1!;
    } else {
      if (widget.t1 == null) {
        X1 = Inventory(0, "", Helper.GetGuid(), "", "", "", widget.sType, "", 1,
            1, 0, UIData.CurrentUser!.Name, "", "", now, uGuid, now, "", "");
      } else {
        X1 = Inventory(
            0,
            "",
            Helper.GetGuid(),
            widget.t1!.TName,
            widget.t1!.TType,
            widget.t1!.SStyle,
            widget.sType,
            widget.t1!.Unit,
            1,
            1,
            0,
            UIData.CurrentUser!.Name,
            widget.t1!.Storage,
            "",
            now,
            uGuid,
            now,
            "",
            "");
      }
    }

    c1.text = "1";
    c2.text = X1.UserName;
    c3.text = X1.Remarks;

    //c3.text=X1.Process;
    //c4.text=X1.Workers;
  }

  upload(BuildContext context) async {
    X1.TName = T1.TName;
    X1.TType = T1.TType;
    X1.Unit = T1.Unit;
    X1.Storage = T1.Storage;
    X1.Remarks = T1.Remarks;
    X1.Amount = Helper.GetDouble(c1.text);
    X1.UserName = c2.text;
    X1.Remarks = c3.text;
    X1.Amount2 = getA2(X1.Amount);
    await Inventory.Add(X1);

    widget.added();
    Navigator.of(context).pop();
  }

  double getA2(double v1) {
    if (X1.SType == 0) return v1;
    if (X1.SType == 1) return -1 * v1;
    if (X1.SType == 2) return -1 * v1;
    return v1;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: InventoryAddBody(context),
    );
  }

  InventoryAddBody(BuildContext context) => SingleChildScrollView(
        child: InventoryAddFields(context),
      );

  void seleted(Things a) {
    this.setState(() {
      T1 = a;
    });
  }

  void selectThing() {
    RouteHelper.ShowPage(context, ThingsSelectPage(true, seleted));
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    if (T1 == null) {
      list4.add(InputWidget.getButton("选择物品", selectThing));
      list1.add(InputWidget.inCard2(list4));
      return list1;
    }
    list2.add(ThingsHelper.thingsCard(T1, selectThing));

    list3.add(InputWidget.getInput("数量", c1));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput("人员", c2));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInput2("备注", c3));
    list3.add(InputWidget.getDivider());

    list4.add(InputWidget.getButton("提交", () {
      upload(context);
    }));
    list1.add(InputWidget.inCard2(list2));
    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));

    return list1;
  }

  InventoryAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
